/**
 * 
 */
package com.vital.mshopping;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import com.vital.dao.Location;

/**
 * @author btan
 *
 */
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem>{
	private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
	private Context context;
	private Location location;
	
	public CustomItemizedOverlay(Drawable defaultMarker) {
		super(boundCenter(defaultMarker));
		// TODO Auto-generated constructor stub
	}
	
	public CustomItemizedOverlay(Drawable defaultMarker, Context context, Location location) {
		this(defaultMarker);
		this.context = context;
		this.location = location;
		// TODO Auto-generated constructor stub
	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return mapOverlays.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mapOverlays.size();
	}

	@Override
	protected boolean onTap(int index) {
		// TODO Auto-generated method stub
		OverlayItem item = mapOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		//---My location with no button show---
		if (this.location != null) {
			dialog.setPositiveButton("See detail", new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Bundle b = new Bundle();
					Intent intent = new Intent(context, DetailLocationActivity.class);
					b.putSerializable("mapObjLocation", location);
					intent.putExtras(b);
					context.startActivity(intent);
					//Toast.makeText(context, location.getId() + "-" + location.getName(), Toast.LENGTH_LONG).show();
				}
			});
		}
		dialog.show();
		return true;
	}
	
	public void addOverlay(OverlayItem item) {
		mapOverlays.add(item);
		this.populate();
	}
	
	
}
